﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_EditCustomers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
           LoadCustomerByID();
    }
    public void LoadCustomerByID()
    {
        if (Request.QueryString.Count > 0)
        {
            int ID = Convert.ToInt32(Request.QueryString["ID"]);
            DataSet2TableAdapters.tbl_customerTableAdapter daCustomer = new DataSet2TableAdapters.tbl_customerTableAdapter();
            DataSet2 ds2 = new DataSet2();
            daCustomer.FillByID(ds2.tbl_customer, ID);
            DataTable dt = new DataTable();
            dt = ds2.tbl_customer;
            FormViewCustomer.DataSource = dt;
            FormViewCustomer.DataBind();

        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Customer objCus = new Customer();
        objCus.CustomerID = Convert.ToInt32(Request.QueryString["ID"]);

        TextBox txtFirstname = (TextBox)FormViewCustomer.FindControl("txtFirstname");
        objCus.Firstname = txtFirstname.Text;
        TextBox txtLastname = (TextBox)FormViewCustomer.FindControl("txtLastname");
        objCus.Lastname = txtLastname.Text;
        //parse string date

        TextBox txtBirthdate = (TextBox)FormViewCustomer.FindControl("txtBirthdate");
        objCus.Birthday = DateTime.Parse(txtBirthdate.Text);

        //checked gender
        DropDownList DropDownListGender = (DropDownList)FormViewCustomer.FindControl("DropDownListGender");
        if (DropDownListGender.SelectedIndex == 1)
            objCus.Gender = true;
        else if (DropDownListGender.SelectedIndex == 2)
            objCus.Gender = false;
        TextBox txtEmail = (TextBox)FormViewCustomer.FindControl("txtEmail");
        objCus.Email = txtEmail.Text;
        TextBox txtPhone = (TextBox)FormViewCustomer.FindControl("txtPhone");
        objCus.Phone = txtPhone.Text;
        TextBox txtAddress = (TextBox)FormViewCustomer.FindControl("txtAddress");
        objCus.Address = txtAddress.Text;
        TextBox txtPassword = (TextBox)FormViewCustomer.FindControl("txtPassword");
        objCus.Password = txtPassword.Text;

            
        CustomerBus CustomerBus = new CustomerBus();
        if (CustomerBus.UpdateCustomer(objCus))
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Messages", "<script>alert('Edit Customer success!')</script>", false);

        }
        else
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Messages", "<script>alert('Edit fail!')</script>", false);
        }
    }

}
